﻿using SteroMood;

namespace SteroClood.MediaPlayers
{
    public class WmpMediaPlayer : IMediaPlayer
    {
        private WMPLib.WindowsMediaPlayer _wmp;
        private EndTackEventHandler _onEndMusicEvent;
        private bool _isPlaying;

        public bool Initialize()
        {
            _isPlaying = false;
            _wmp = new WMPLib.WindowsMediaPlayer();
            if (_wmp == null)
            {
                return false;
            }

            _wmp.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(wmp_PlayStateChange);
            _wmp.MediaError += new WMPLib._WMPOCXEvents_MediaErrorEventHandler(wmp_MediaError);
            return true;
        }

        void wmp_MediaError(object pMediaObject)
        {
            MediaEnded();
        }

        void wmp_PlayStateChange(int NewState)
        {
            if (NewState == 8 /*MediaEnded*/)
            {
                MediaEnded();
            }
        }

        private void MediaEnded()
        {
            if (_onEndMusicEvent != null)
            {
                _isPlaying = false;
                _onEndMusicEvent();
            }
        }

        public void Shutdown()
        {
            _wmp.controls.stop();
        }

        public bool LoadSong(string pathOrUrl)
        {
            _wmp.controls.stop();
            _wmp.close();
            _wmp.URL = pathOrUrl;
            _wmp.controls.play();
            _isPlaying = true;
            return true;
        }

        public bool PlayPause()
        {
            if(_isPlaying)
            {
                _wmp.controls.pause();
            }
            else
            {
                _wmp.controls.play();
            }
            _isPlaying = !_isPlaying;
            return _isPlaying;
        }

        public void SetVolume(int volume)
        {
            _wmp.settings.volume = volume;
        }

        public MediaPlayerStatus GetStatus()
        {
            MediaPlayerStatus mediaPlayerStatus = new MediaPlayerStatus();

            mediaPlayerStatus.SongLengthInMs = (long)_wmp.currentMedia.duration * 1000;
            mediaPlayerStatus.SongPositionInMs = (long)_wmp.controls.currentPosition * 1000;
            switch (_wmp.playState)
            {
                /*
                    0 	MpStopped 	Playback is stopped.
                    1 	MpPaused 	Playback is paused.
                    2 	MpPlaying 	Stream is playing.
                    3 	MpWaiting 	Waiting for stream to begin.
                    4 	MpScanForward 	Stream is scanning forward.
                    5 	MpScanReverse 	Stream is scanning in reverse.
                    6 	MpSkipForward 	Skipping to next.
                    7 	MpSkipReverse 	Skipping to previous.
                    8 	MpClosed 	Stream is not open.
                 */
                case WMPLib.WMPPlayState.wmppsPaused:
                    mediaPlayerStatus.IsPlaying = true;
                    mediaPlayerStatus.IsPaused = true;
                    mediaPlayerStatus.Status = "Paused";
                    break;
                case WMPLib.WMPPlayState.wmppsPlaying:
                    mediaPlayerStatus.IsPlaying = true;
                    mediaPlayerStatus.IsPaused = false;
                    mediaPlayerStatus.Status = "Playing";
                    break;
                case WMPLib.WMPPlayState.wmppsWaiting:
                    mediaPlayerStatus.IsPlaying = false;
                    mediaPlayerStatus.IsPaused = false;
                    mediaPlayerStatus.Status = "Waiting";
                    break;
                case WMPLib.WMPPlayState.wmppsBuffering:
                    mediaPlayerStatus.IsPlaying = false;
                    mediaPlayerStatus.IsPaused = false;
                    mediaPlayerStatus.Status = "Buffering";
                    break;
                case WMPLib.WMPPlayState.wmppsTransitioning:
                    mediaPlayerStatus.IsPlaying = false;
                    mediaPlayerStatus.IsPaused = false;
                    mediaPlayerStatus.Status = "Loading...";
                    break;
                case WMPLib.WMPPlayState.wmppsReady:
                    mediaPlayerStatus.IsPlaying = false;
                    mediaPlayerStatus.IsPaused = false;
                    mediaPlayerStatus.Status = "Ready";
                    break;
                default:
                    mediaPlayerStatus.IsPlaying = false;
                    mediaPlayerStatus.IsPaused = false;
                    mediaPlayerStatus.Status = "Stopped";
                    break;
            }

            string pos = string.Format("{0:00}:{1:00}", mediaPlayerStatus.SongPositionInMs / (60 * 1000), (mediaPlayerStatus.SongPositionInMs / 1000) % 60);
            string length = string.Format("{0:00}:{1:00}", mediaPlayerStatus.SongLengthInMs / (60 * 1000), (mediaPlayerStatus.SongLengthInMs / 1000) % 60);
            mediaPlayerStatus.Status += " " + pos + " / " + length;
            return mediaPlayerStatus;
        }

        public EndTackEventHandler OnEndMusicEvent
        {
            get
            {
                return _onEndMusicEvent;
            }
            set
            {
                _onEndMusicEvent += value;
            }
        }
    }
}
